// pages/page2/index.js

const app = getApp()
Page({
    data: {
        list: [],
        age: 18
    },

    handleButtonTap() {
        console.log(app.msg)
    },

    handleRequest() {
        wx.showLoading({
            title: '请求书籍列表中...',
            mask: true
        })
        wx.request({
            url: 'https://hmajax.itheima.net/api/books',
            method: 'GET',
            data: {
                creator: 'zhangsan'
            },
            success: (res) => {
                console.log('网络请求成功', res)
                this.setData({
                    list: res.data.data
                })
                console.log(this.data.list)
            },
            fail: (res) => {
                console.log('网络请求失败', res)
            },
            complete: (res) => {
                console.log('网络请求完成', res)
                wx.hideLoading()
            }
        })
    },
    handleLoading() {
        wx.showLoading({
            title: '数据加载xxx',
            mask: true
        })

        setTimeout(() => {
            wx.hideLoading()
        }, 1500)
    },
    handleToast() {
        wx.showToast({
            title: '你好',
            icon: 'none',
            duration: 3000,
            mask: true
        })
    },

    handleAge() {
        this.setData({
            // age: ++this.data.age,
            // age: this.data.age++
            age: this.data.age + 1
            // age: this.data.age - 1
        })
    },
    saveData() {
        wx.setStorageSync('token', 'tokenxxxx')
        wx.setStorageSync('name', 'Jack')
        let obj = {
            name: 'Rose',
            age: 18
        }
        // 和浏览器一致
        wx.setStorageSync('obj', JSON.stringify(obj))

        // 小程序的特殊点，可以直接存储一个对象
        wx.setStorageSync('obj2', obj)

    },
    readData() {
        console.log(wx.getStorageSync('token'))

        // // parse成js可用的对象
        // let obj = JSON.parse(wx.getStorageSync('obj'))
        // console.log(typeof obj, obj, obj.name)

        // // 小程序的特殊点
        // let obj2 = wx.getStorageSync('obj2')
        // console.log(typeof obj2, obj2, obj2.name)
    },
    delData() {
        wx.removeStorageSync('token')
    },
    clearData() {
        // 全删
        wx.clearStorageSync()
    },
})